$(function () {
    var emp_datagrid = $("#emp_datagrid");
    var emp_dialog = $("#emp_dialog");
    var emp_form = $("#emp_form");

    emp_datagrid.datagrid({
        url:'/employeeInfo/query.do',
        fit:true,
        fitColumns:true,

        columns:[
            [
                {field:'workerNumber',title:'工号',width:100},
                {field:'name',title:'真实名字',width:100},
                {field:'department',title:'部门',width:100,formatter:function (value) {
                        return value ? value.name:"";
                    }},
                {field:'title',title:'职务',width:100,formatter:function (value) {
                        return value ? '老板' : '员工'}},
                {field:'employment',title:'聘用形式',width:100,formatter:function (value) {
                        return value ? '合同' : '非合同'}},
                {field:'educationId',title:'学历',width:100,formatter:function (value) {
                        return value ? '专科' : '本科'}
                },
                {field:'job',title:'岗位',width:100,sortable:true},
                {field:'gender',title:'性别',width:100,formatter:function (value) {
                        return value ? '女' : '男'
                    }},
                {field:'age',title:'年龄',width:100},
                {field:'national',title:'籍贯',width:100},
                {field:'tel',title:'电话',width:100}
            ]
        ],
        pagination:true,
        rownumbers:true,
        singleSelect:true,
        toolbar:'#tb',
    })

    emp_dialog.dialog({
        width:900,
        height:500,
        buttons:'#form_btn',
        title:'员工信息',
        closed:true,
        onClose:function () {
            emp_form.form('clear')
            $("#sal_form").form('clear')
        }
    })

    var methodObj = {
        add:function() {
            emp_dialog.dialog('open');
            emp_dialog.dialog('setTitle','新增员工');

        },
        edit:function() {
            var row = emp_datagrid.datagrid('getSelected');
            if(!row){
                $.messager.alert('温馨提示','请选择一条数据','warning');
                return;
            }

            if(row.department){
                row["department.id"] = row.department.id;
            }
            emp_form.form('load',row);
            emp_dialog.dialog('open');
            emp_dialog.dialog('setTitle','编辑员工');
            var workNumber = row.workerNumber;
            if (workNumber){
                $.post('/salary/selectByJopNumber.do',{jopNumber:workNumber},function (data) {
                    $("#sal_form").form('load',data);
                    $("#dept").combobox('setValue',data.dept.name);
                    $("#title").combobox('setValue',data.title.name);
                    $("#edu").combobox('setValue',data.educationId.name);;
                });
            }
        },
        reload:function() {
            emp_datagrid.datagrid('reload');
        },
        save:function() {
            emp_form.form("submit",{
                url:'/employeeInfo/saveOrUpdate.do',
                success:function (data) {
                    data = $.parseJSON(data);
                    if(data.success){
                        $.messager.alert("温馨提示","保存成功",'info',function () {
                            methodObj['cancel']();
                            methodObj['reload']();
                        })
                    }else{
                        $.messager.alert("温馨提示","保存失败",'warning')
                    }
                }
            })
        },
        cancel:function () {
            emp_dialog.dialog('close')
        },
        query:function() {
            var name = $("[name=name]").val();
            var workerNumber = $("[name=workerNumber]").val();
            var employment = $("[name=employment]").val();
            var title = $("[name=title]").val();
            var educationId = $("[name=educationId]").val();

            emp_datagrid.datagrid('reload',{
                name:name,
                workerNumber:workerNumber,
                employment:employment,
                title:title,
                educationId:educationId
            })

        },
        exportXls:function () {
            window.location.href = "/employee/exportXls.do";
        }
    }

    $("a[data-cmd]").click(function () {
        var methodName = $(this).data("cmd");
        methodObj[methodName]();
    })
})


